﻿using SHRIS.MIS.Common.Enum.SystemManage;
using SHRIS.MIS.Common.Model;
using SHRIS.MIS.Common.Util.Extension;
using SHRIS.MIS.Common.Util.Model;
using SHRIS.MIS.Entity.SystemManage;
using SHRIS.MIS.IService.SystemManage;
using SHRIS.MIS.Model.Param.SystemManage;
using SHRIS.MIS.Service.Cache;
using SHRIS.MIS.Service.SystemManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHRIS.MIS.Business.SystemManage
{
    public class RoleBLL
    {
        private IRoleService _roleService ;
        private MenuAuthorizeService _menuAuthorizeService;

        private MenuAuthorizeCache _menuAuthorizeCache;
   
        public RoleBLL(IRoleService roleService, MenuAuthorizeService menuAuthorizeService, MenuAuthorizeCache menuAuthorizeCache)
        {
            _roleService = roleService;
            _menuAuthorizeService = menuAuthorizeService;
            _menuAuthorizeCache = menuAuthorizeCache;
        }

        #region 获取数据
        public async Task<TEntity<List<RoleEntity>>> GetList(RoleListParam param)
        {
            TEntity<List<RoleEntity>> obj = new TEntity<List<RoleEntity>>();
            obj.Data = await _roleService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<List<RoleEntity>>> GetPageList(RoleListParam param, Pagination pagination)
        {
            TEntity<List<RoleEntity>> obj = new TEntity<List<RoleEntity>>();
            obj.Data = await _roleService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<RoleEntity>> GetEntity(Guid id)
        {
            TEntity<RoleEntity> obj = new TEntity<RoleEntity>();
            RoleEntity roleEntity = await _roleService.GetEntity(id);
            List<MenuAuthorizeEntity> menuAuthorizeList = await _menuAuthorizeService.GetList(new MenuAuthorizeEntity
            {
                AuthorizeId = id,
                AuthorizeType = AuthorizeTypeEnum.Role.ParseToInt()
            });
            // 获取角色对应的权限
            if (menuAuthorizeList.Count ==0)
            {
                roleEntity.MenuIds = string.Empty;
            }
            else
            {
                roleEntity.MenuIds = string.Join(",", menuAuthorizeList.Select(p => p.MenuId));
            }
            

            obj.Data = roleEntity;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<int>> GetMaxSort()
        {
            TEntity<int> obj = new TEntity<int>();
            obj.Data = await _roleService.GetMaxSort();
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TEntity<string>> SaveForm(RoleEntity entity)
        {
            TEntity<string> obj = new TEntity<string>();

            if (_roleService.ExistRoleName(entity))
            {
                obj.Message = "角色名称已经存在！";
                return obj;
            }

            await _roleService.SaveForm(entity);

            // 清除缓存里面的权限数据
            _menuAuthorizeCache.Remove();

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;

            return obj;
        }

        public async Task<TEntity> DeleteForm(string ids)
        {
            TEntity obj = new TEntity();

            await _roleService.DeleteForm(ids);

            // 清除缓存里面的权限数据
            _menuAuthorizeCache.Remove();

            obj.Tag = 1;
            return obj;
        }
        #endregion

    }
}
